package com.cskaoyan.wordmemorize.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cskaoyan.wordmemorize.converter.LevelConverter;
import com.cskaoyan.wordmemorize.dao.entity.LevelPrivilegeDO;
import com.cskaoyan.wordmemorize.dao.mapper.LevelMapper;
import com.cskaoyan.wordmemorize.dao.mapper.LevelPrivilegeMapper;
import com.cskaoyan.wordmemorize.dto.admin.DeleteLevelPrivilegeOperateDTO;
import com.cskaoyan.wordmemorize.dto.admin.LevelPrivilegeDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.request.BatchDeleteCommand;
import com.cskaoyan.wordmemorize.request.LevelPrivilegeCommand;
import com.cskaoyan.wordmemorize.request.LevelPrivilegePageRequest;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.service.LevelPrivilegeService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class LevelPrivilegeServiceImpl implements LevelPrivilegeService {
    @Resource
    LevelPrivilegeMapper levelPrivilegeMapper;
    @Resource
    LevelMapper levelMapper;
    @Resource
    LevelConverter levelConverter;
    // 分页查询(后台等级权限管理-特权管理)
    @Override
    public PageDTO<LevelPrivilegeDTO> getLevelPrivilegeList(LevelPrivilegePageRequest pageRequest) {
        String levelId = pageRequest.getLevelId();
        String keyword = pageRequest.getKeyword();
        LambdaQueryWrapper<LevelPrivilegeDO> queryWrapper=new LambdaQueryWrapper<>();
        Page page=new Page(pageRequest.getPageNum(), pageRequest.getCount());
        List<LevelPrivilegeDO> levelPrivilegeDOS = levelPrivilegeMapper.selectList(null);
        if(levelId!=null||keyword!=null){
            queryWrapper.eq(LevelPrivilegeDO::getLevelId,levelId)
                    .or()
                    .like(LevelPrivilegeDO::getPrivilegeCode,pageRequest.getKeyword())
                    .or()
                    .like(LevelPrivilegeDO::getPrivilegeName,pageRequest.getKeyword());

            // List list = levelPrivilegeMapper.selectList(page, queryWrapper);
            // List selectRecords=page.getRecords();
            // return levelConverter.levelPrivilegeDOToPageDTO(selectRecords,list.size());
        }
        Page selectPage = levelPrivilegeMapper.selectPage(page, queryWrapper);
        List records = page.getRecords();
        return levelConverter.levelPrivilegeDOToPageDTO(selectPage.getRecords(),selectPage.getTotal());


    }
    // 添加等级特权(后台等级权限管理-特权管理)
    @Override
    public void addLevelPrivilege(LevelPrivilegeCommand command) {
        LevelPrivilegeDO levelPrivilegeDO = levelConverter.levelPrivilegeCommandToLevelPrivilegeDO(command);
        // LambdaQueryWrapper<LevelPrivilegeDO> queryWrapper = new LambdaQueryWrapper<>();
        // queryWrapper.eq(LevelPrivilegeDO::getIsDeleted,1);
        List<LevelPrivilegeDO> levelPrivilegeDOS = levelPrivilegeMapper.selectList(null);
        Optional<Long> reduce = levelPrivilegeDOS.stream().map(o -> o.getId()).reduce((o1, o2) -> o1 > o2 ? o1 : o2);
        levelPrivilegeDO.setId(reduce.get()+1);
        levelPrivilegeMapper.insert(levelPrivilegeDO);
    }
    // 更新等级特权(后台等级权限管理-特权管理)
    @Override
    public void updateLevelPrivilege(LevelPrivilegeCommand command) {
        LevelPrivilegeDO levelPrivilegeDO = levelConverter.levelPrivilegeCommandToLevelPrivilegeDO(command);
        levelPrivilegeMapper.updateById(levelPrivilegeDO);
    }
    // 删除等级特权(后台等级权限管理-特权管理)
    @Override
    public DeleteLevelPrivilegeOperateDTO deleteLevelPrivilege(Long id) {
        LevelPrivilegeDO levelPrivilegeDO = levelPrivilegeMapper.selectById(id);
        levelPrivilegeMapper.deleteById(id);
        return levelConverter.levelPrivilegeDOToDeleteLevelPrivilegeOperateDTO(levelPrivilegeDO);
    }
    // 批量删除等级特权(后台等级权限管理-特权管理)
    @Override
    public List<DeleteLevelPrivilegeOperateDTO> batchDeleteLevelPrivilege(BatchDeleteCommand command) {
        List<LevelPrivilegeDO> levelPrivilegeDOS = levelPrivilegeMapper.selectByIds(command.getIds());
        levelPrivilegeMapper.deleteByIds(command.getIds());
        return levelConverter.levelPrivilegeDOsToDeleteLevelPrivilegeOperateDTOs(levelPrivilegeDOS);
    }
}
